<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Verification\Admin\Login;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class LoginController extends Controller
{

    /**
     *  后台用户登录
     */
    public function login(Login $request){
        //接收POST参数 并验证
        $input = $request->all();
        //查询账号密码是否匹配
        $user = DB::table('admin_user')->where('username', $input['username'])->first();
        if($user){
            if(md5($input['password']) != $user->password){
                self::$response['msg']='密码错误';
            }else{
                //更新token和token过期时间
                $arr = [
                    'token'=>$this->createToken(),
                    'token_expire'=>date('Y-m-d H:i:s',time()+1800),
                ];
                DB::table('admin_user')->where('username', $input['username'])->update($arr);

                self::$response['code']= 1;
                self::$response['data']= DB::table('admin_user')->where('username', $input['username'])->first();;
            }
        }else{
            self::$response['msg']='账号不存在';
        }

        return self::$response;
    }

    /**
     * 退出登录
     */
    public function loginOut(Request $request){
        $input =  $request->input();//获取参数
        $arr = [
            'token'=>0,
            'token_expire'=>'1970-01-01 00:00:00',
        ];
        DB::table('admin_user')->where('id',$input['LoginUid'])->update($arr);
        self::$response['code'] = 1;
        return self::$response;
    }
}